package com.wxm.remotemeter.service.impl;

import java.util.Map;

import com.talkweb.ncfw.data.Page;
import com.talkweb.ncfw.exceptions.BizBaseException;
import com.talkweb.ncfw.exceptions.BizMultiException;
import com.talkweb.ncfw.exceptions.DAOBaseException;
import com.talkweb.ncfw.orm.IGenericDAO;
import com.talkweb.ncfw.utils.AssertUtils;
import com.talkweb.ncfw.utils.PKUtils;
import com.wxm.remotemeter.persistence.entity.Concentrator;
import com.wxm.remotemeter.service.IConcentratorService;

/**
 * <p>文件名称: ConcentratorServiceImpl.java</p>
 * <p>文件描述: 集中器服务实现类</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2012-2-20</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class ConcentratorServiceImpl implements IConcentratorService {

	private static final String DEFAULT_NAMESPACE = "com.wxm.remotemeter.persistence.dao.ConcentratorMapper";
	private IGenericDAO genericDAO;
	
	public IGenericDAO getGenericDAO() {
		return genericDAO;
	}

	public void setGenericDAO(IGenericDAO genericDAO) {
		this.genericDAO = genericDAO;
	}

	public void add(Concentrator... concentrators) throws BizBaseException {
		String[] concentratoridArray = PKUtils.getPrimaryKeyArray(concentrators.length);
		for (int i = 0; i < concentrators.length; i++) {
			concentrators[i].setConcentratorid(concentratoridArray[i]);
		}
		try {
			this.genericDAO.insert(DEFAULT_NAMESPACE + ".insert", concentrators);
		} catch (DAOBaseException e) {
			if (concentrators.length == 0) {
				throw new BizBaseException("添加集中器信息时发生错误", e);
			} else {
				throw new BizMultiException("添加集中器信息时发生错误", e);
			}
		}
	}

	public void update(Concentrator concentrator) throws BizBaseException {
		try {
			this.genericDAO.update(DEFAULT_NAMESPACE + ".update", new Object[]{concentrator});
		} catch (DAOBaseException e) {
			throw new BizBaseException("修改集中器信息时发生错误", e);
		}
	}

	public void deleteBatch(String[] concentratoridArray)
			throws BizBaseException {
		AssertUtils.notEmpty(concentratoridArray, " 集中器ID不能为空");
		try {
			this.genericDAO.deleteByPkArray(DEFAULT_NAMESPACE + ".deleteByPkArray", concentratoridArray);
		} catch (DAOBaseException e) {
			throw new BizBaseException("修改集中器信息时发生错误", e);
		}
	}

	public Concentrator get(String concentratorid) throws BizBaseException {
		Object concentratorObj = null;
		try {
			concentratorObj = this.genericDAO.load(DEFAULT_NAMESPACE + ".getByPrimaryKey", concentratorid);
		} catch (DAOBaseException e) {
			throw new BizBaseException("获取集中器信息时发生错误，集中器ID[" + concentratorid + "]", e);
		}
		return concentratorObj == null ? null : (Concentrator)concentratorObj;
	}

	public Page<Concentrator> find(Page<Concentrator> page,
			Map<String, Object> condition) throws BizBaseException {
		try {
			return this.genericDAO.find(DEFAULT_NAMESPACE + ".getCount", DEFAULT_NAMESPACE + ".find", 
					page, condition);
		} catch (DAOBaseException e) {
			throw new BizBaseException("查询集中器时发生错误", e);
		}
	}

}

